<?php

namespace App\Models\Archive\Migration;

use App\Models\Common\ArchiveCatalog;
use App\Models\Common\BaseMigration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Psr\Log\LoggerInterface;
use function app;

class ParkOutLogsTable extends BaseMigration
{
    const TABLE_NAME = 'park_out_logs';
    protected $connection = self::DB_CONN;
    const SPLIT_MODE = self::SPLIT_MODE_OPTION_YEAR;

    public function up(string $tableName): bool
    {
        $success = false;

        try {
            $conn = Schema::connection($this->connection);

            if (!$conn->hasTable($tableName)) {
                $conn->create($tableName, function (Blueprint $table) {
                    $table->uuid('id')->primary();
                    $table->string('car_no', 20);
                    $table->string('car_no_type', 50)->default('');
                    $table->string('car_type', 20);
                    $table->dateTime('enter_at')->nullable()->index('idx_enter_at');
                    $table->string('enter_pic')->default('');
                    $table->boolean('enter_is_open')->default(false);
                    $table->string('enter_lane_name', 50)->default('');
                    $table->uuid('enter_lane_id')->nullable()->index('idx_enter_lane');
                    $table->string('enter_admin_name', 50)->default('');
                    $table->uuid('enter_admin_id')->nullable();
                    $table->dateTime('out_at')->nullable()->index('idx_out_at');
                    $table->string('out_pic')->default('');
                    $table->boolean('out_is_open')->default(false);
                    $table->string('out_lane_name', 50)->default('');
                    $table->uuid('out_lane_id')->nullable()->index('idx_out_lane');
                    $table->string('out_admin_name', 50)->default('');
                    $table->uuid('out_admin_id')->nullable();
                    $table->string('area_name', 50)->default('');
                    $table->uuid('area_id')->nullable()->index('idx_area_id');
                    $table->uuid('mth_card_id')->nullable();
                    $table->string('mth_card_category', 10)->default('');
                    $table->uuid('store_car_id')->nullable();
                    $table->dateTime('lock_at')->nullable();
                    $table->string('remark')->default('');
                    $table->json('rule')->nullable();
                    $table->uuid('parking_lot_id');
                    $table->string('rid')->default('');
                    $table->timestamps();
                    $table->index(['parking_lot_id', 'car_no', 'car_type'], 'idx_parking_lot_car_no');
                });
            }
            $success = true;
        } catch (\Throwable $exception) {
            app(LoggerInterface::class)->error($exception->getMessage(), ['exception' => $exception, 'act' => '创建出场车辆记录分表']);
        } finally {
            try {
                $success && ArchiveCatalog::insertOrIgnore(['name' => $tableName]);
            } catch (\Throwable $exception) {
                app(LoggerInterface::class)->error($exception->getMessage(), ['exception' => $exception, 'act' => '更新存档菜单表']);
            }
        }

        return $success;
    }
}
